package com.daohang361.imqq.ui.file;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;

import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import com.csu.client.logger.EIMLogger;
import com.csu.client.module.network.socket.ClientFileSocket;
import com.csu.message.FileRequestMessage;
import com.csu.message.FileResponseMessage;
import com.csu.message.MessageType;
import com.daohang361.imqq.resourse.ResourcesManagement;

public class SendGetFileNode extends JPanel {

	Box box=Box.createVerticalBox();
	JProgressBar jp_sendGetProgress;
	FileRequestMessage message;
	JLabel saveButton;
	JLabel refusebutton;
	JLabel ompletionLabel;
	JLabel speedLabel;
	Box fileInfoBox;
	Box fileGetInfoBox;
	Box userActionbox;
	SendGetFilePanle container;
	boolean isGet;
	
	public SendGetFileNode(boolean isGet,FileRequestMessage msg){
		message=msg;
		String fileName =message.getFileName();
		this.isGet=isGet;
		fileInfoBox=this.getFileInfoBox(fileName);
		userActionbox=this.getUserAction(isGet);
		box.add(fileInfoBox);
		box.add(userActionbox);
		this.setBackground(Color.white);
		this.add(box,BorderLayout.CENTER);
		saveButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                      saveAction();
             }});
		refusebutton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
            	  refuseAction();
             }});
	}
	
	private void saveAction() {
		//-----------操作界面--------------
		userActionbox.setVisible(false);
		fileGetInfoBox=this.getFileGetInfoBox();
		fileGetInfoBox.setVisible(true);
		box.add(fileGetInfoBox);
	    //-----------操作消息--------------
		String friendQQ=message.getSrcQQ();
		String owerQQ=message.getDestQQ();
		FileResponseMessage responseMessage=new FileResponseMessage(owerQQ,friendQQ);
		responseMessage.setFileLength(message.getFileLength());
		responseMessage.setFileAbsolutePath(message.getFileAbsolutePath());
		responseMessage.setFileName(message.getFileName());
		File file=new File("C:\\Documents and Settings\\Administrator\\My Documents"+"\\"+message.getFileName());
		responseMessage.setFileResponse(MessageType.FileResponse_Accepted);
		javax.swing.JFileChooser fileChooser = new javax.swing.JFileChooser();
		fileChooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
		fileChooser.showOpenDialog(this);
	    file = fileChooser.getCurrentDirectory();
	    String saveFilePatn=file.getAbsolutePath()+"\\"+message.getFileName();
	    responseMessage.setSaveFilePath(saveFilePatn);
	    EIMLogger.Error("此用户保存的路劲为："+saveFilePatn);
	    ClientFileSocket fileSocket=ClientFileSocket.getInstance();
	    try {
			fileSocket.startSocket();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void refuseAction() {
		//-----------操作界面--------------
		this.setVisible(false);
		this.getContainer().removePanle(this);
		 //-----------操作消息--------------
	}
	
	private Box getFileInfoBox(String fileName){
		Box box=Box.createHorizontalBox();
		String ext=ResourcesManagement.getFileExtractor(fileName);
		Icon icon=ResourcesManagement.getIconByExtractor(ext, true);
		JLabel fileImage=new JLabel(icon);
		box.add(fileImage,BorderLayout.WEST);
		Box box2=Box.createVerticalBox();
		JLabel info=new JLabel("文件请求");
		JLabel fileNameLabel=new JLabel(fileName);
		box2.add(info);
		box2.add(fileNameLabel,BorderLayout.WEST);
		box.add(new JLabel("    "));
		box.add(box2,BorderLayout.EAST);
		return box;
	}
	
	private Box getFileGetInfoBox(){
		Box box=Box.createVerticalBox();
		jp_sendGetProgress =new JProgressBar();
		jp_sendGetProgress.setBackground(Color.white);
		jp_sendGetProgress.setForeground(Color.black);
		box.add(jp_sendGetProgress);
		ompletionLabel =new JLabel("完成 :0%");
		speedLabel=new JLabel("速度: 350k/s");
		Box box4=Box.createHorizontalBox();
		box4.add(ompletionLabel,BorderLayout.WEST);
		box4.add(new JLabel("    "));
		box4.add(refusebutton);
		box4.add(new JLabel("    "));
		box4.add(speedLabel,BorderLayout.EAST);
		box.add(box4);
		return box;
	}
	
	
	private Box getUserAction(boolean isGet){
		Box box=Box.createHorizontalBox();
		if(isGet){
			saveButton=new JLabel("<html><a href=\" \">另存为</a></html>");
			refusebutton=new JLabel("<html><a href=\" \">拒绝</a></html>");
			box.add(saveButton,BorderLayout.WEST);
			box.add(refusebutton,BorderLayout.EAST);
			
		}else{
			saveButton=new JLabel("<html><a href=\" \">发送离线文件</a></html>");
			refusebutton=new JLabel("<html><a href=\" \">取消</a></html>");
			box.add(saveButton,BorderLayout.WEST);
			box.add(new JLabel("      "));
			box.add(new JLabel("      "));
			box.add(refusebutton,BorderLayout.EAST);
		}
		return box;
	}

	public SendGetFilePanle getContainer() {
		return container;
	}

	public void setContainer(SendGetFilePanle container) {
		this.container = container;
	}
	
	
	
	
	
}